<!--#include file="./header.inc"-->
<%

select case aq
	'product
	case "save" 			: 	call save
	case "edit" 			: 	call edit
	case "delete" 			: 	call delete
	case "status" 			: 	call status
	case "editbatch" 		: 	call editBatch
	case "savebatch" 		: 	call saveBatch
	case else 				: 	call index
end select

call db.close()

function index()
	dim keyword, i
	classID = admin.joinID(request("classID"))
	keyword = admin.filter(request("keyword"))
	sql = "select id,classID,title,productType,price,hits,picUrl,isStop from cj_product where 1=1 "
	if keyword <> "" then sql = sql & " and(title like '%" & keyword & "%' or content like '%" & keyword & "%')"
	if classID <> 0 then sql = sql & " and classID in(" & classID & ")"
	sql = sql & " Order by id desc"
	temp = db.arrayData(sql, page, rows)
	infoTotal = temp(0)
	data = temp(1)
	%><!--#include file="./view/product.index.html"--><%
end function

function edit()
	data = array("", "", siteTitle, "", now(), "", "", "", "", "", "", "", "", "")
	if iid > 0 then
		set rs = db.exec("select title,productType,manufacturer,price,dateTime,proUrl1,proUrl2,hits,picUrl,infoViewID,classID,isStop,content from cj_product where id=" & iid, 1)
		if rs.eof then admin.message "id不存在！"
		data = rs
		set rs = nothing
	end if
	%><!--#include file="./view/product.edit.html"--><%
end function

function save()
	title = admin.filter(request.form("title"))
	productType = admin.filter(request.form("productType"))
	manufacturer = admin.filter(request.form("manufacturer"))
	content = trim(request.form("content"))
	if title = "" then admin.message "产品名称不能空！"
	if len(content) < 10 then admin.message "产品介绍内容字数太少了！"
	content = admin.enUploadDir(content)
	if manufacturer = "" then manufacturer = siteTitle
	if iid > 0 then
		set rs = db.exec("select * from cj_product where id=" & iid, 3)
		if rs.eof then admin.message "id不存在！"
	else
		set rs = db.exec("select * from cj_product", 3)
		rs.AddNew
	end if
	rs("classID") = app.int(request.form("classID"))
	rs("title") = title
	rs("productType") = productType
	rs("manufacturer") = manufacturer
	rs("content") = content
	rs("price") = admin.formatPrice(request.form("price"))
	rs("dateTime") = app.cj_date(request.form("dateTime"))
	rs("hits") = app.int(request.form("hits"))
	rs("picUrl") = admin.filter(request.form("picUrl"))
	rs("isStop") = admin.isCheck(request.form("isStop"))
	rs("infoViewID") = app.int(request.form("infoViewID"))
	rs("proUrl1") = trim(request.form("proUrl1"))
	rs("proUrl2") = trim(request.form("proUrl2"))
	rs.update
	rs.close : set rs = nothing
	if iid > 0 then
		admin.back "修改成功！", "返回列表", "?q=index"
	else
		admin.back "添加成功！", "继续添加", "?q=edit"
	end if
end function

function editBatch()
	%><!--#include file="./view/product.editBatch.html"--><%
end function

function saveBatch()
	dim cid, isClassID, isInfoViewID, isHits, isDateTime, isManufacturer, isProductType, isPrice, isProUrl1, proUrl1, isProUrl2, proUrl2, iInfoViewID

	isHits = admin.isCheck(request.form("isHits"))
	isClassID = admin.isCheck(request.form("isClassID"))
	isInfoViewID = admin.isCheck(request.form("isInfoViewID"))
	isDateTime = admin.isCheck(request.form("isDateTime"))
	isManufacturer = admin.isCheck(request.form("isManufacturer"))
	isProductType = admin.isCheck(request.form("isProductType"))
	isPrice = admin.isCheck(request.form("isPrice"))
	isProUrl1 = admin.isCheck(request.form("isProUrl1"))
	isProUrl2 = admin.isCheck(request.form("isProUrl2"))
	if not(isHits) and not(isPrice) and not(isClassID) and not(isDateTime) and not(isInfoViewID) and not(isManufacturer) and not(isProductType) and not(isProUrl1) and not(isProUrl2) then admin.message "需要选择一项！"
	if isClassID then
		cid = app.int(request.form("classID"))
		if isArray(app.getClassInfo(cid)) then
			if app.getClassInfo(cid)(1) <> 1 then admin.message "不属于产品类栏目分类！"
		else
			admin.message "栏目分类不存在！"
		end if
	end if
	if isInfoViewID then
		iInfoViewID = app.int(request.form("infoViewID"))
	end if
	dim dateTime, hits, manufacturer, productType, price
	dateTime = app.cj_date(request.form("dateTime"))
	hits = app.int(request.form("hits"))
	manufacturer = admin.filter(request.form("manufacturer"))
	productType = admin.filter(request.form("productType"))
	price = admin.formatPrice(request.form("price"))
	proUrl1 = trim(request.form("proUrl1"))
	proUrl2 = trim(request.form("proUrl2"))
	set rs = db.exec("select * from cj_product where id in(" & batchId & ")", 3)
	do until rs.eof
		if isClassID then rs("classID") = cid
		if isInfoViewID then rs("infoViewID") = iInfoViewID
		if isDateTime then rs("dateTime") = dateTime
		if isHits then rs("hits") = hits
		if isManufacturer then rs("manufacturer") = manufacturer
		if isProductType then rs("productType") = productType
		if isPrice then rs("price") = price
		if isProUrl1 then rs("proUrl1") = proUrl1
		if isProUrl2 then rs("proUrl2") = proUrl2
		rs.update
		rs.movenext
	loop
	rs.close : set rs = nothing
	admin.back "修改成功！", "返回列表首页", "?q=index"
end function

function delete()
	call db.exec("Delete from cj_product where id in(" & batchId & ")", 1)
	app.json "删除成功！", 0
end function

function status()
	dim isStop, msg
    isStop = app.cj_bool(request("isStop"))
    msg = "启用"
    if isStop then
        msg = "禁用"
    end if
	call db.exec("update cj_product set isStop=" & isStop & " where id in(" & batchId & ")", 1)
	app.json msg & "成功！", 0
end function

%>